﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace VASJ.Service.Library.Manager
{
    public class ForgotValidateManager : BaseManager
    {
        /// <summary>
        /// Init
        /// </summary>
        /// <param name="_ApplicationName"></param>
        public ForgotValidateManager(string _ApplicationName)
            : base(_ApplicationName)
        {

        }

        /// <summary>
        /// Insert code allow forgot password of user
        /// </summary>
        /// <returns></returns>
        public bool InsertForgotPass(string code, string userName, string newPass)
        {
            try
            {
                AHSForgotValidate it = new AHSForgotValidate();

                it.Code = code;
                it.Username = userName;
                it.Newpass = newPass;

                this.mdb.AHSForgotValidates.InsertOnSubmit(it);

                this.mdb.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                this.mdb.RejectChanges();

                Logger.Log.ErrorFormat("[" + this.ApplicationName + "] ForgotValidateManager.InsertForgotPass error {0}", ex);

                return false;
            }
        }

        /// <summary>
        /// Delete all forgot password by userName
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool DeleteAllForgotByUser(string userName)
        {
            try
            {
                UserManager um = new UserManager(this.ApplicationName);

                if (um.IsExistUser(userName))
                {
                    var fvs = from fv in this.mdb.AHSForgotValidates
                              where fv.Username.ToLower() == userName.ToLower()
                              select fv;

                    if (fvs != null)
                    {
                        this.mdb.AHSForgotValidates.DeleteAllOnSubmit(fvs);

                        this.mdb.SubmitChanges();
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + this.ApplicationName + "] ForgotValidateManager.DeleteAllForgotByUser error {0}", ex);

                return false;
            }
        }
    }
}